﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class FLTDYEORDER : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        FLTDYEORDERData m_Data, m_DataX;
        public DataSet setUI=new DataSet();
        public DataSet dsParamaters;
        public DataRow dwABPOS;
        public DataRow dwVKPOS;
        public INTEXDBC INTEXDBC;
        private DataRow dwUI;
        DataTable tableUI;
        private string fromdate = "", enddate = "";   
        #endregion
        public FLTDYEORDER()
        {
            InitializeComponent();
            this.Name = "Dye_Order";
           
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new FLTDYEORDERData();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
            tableUI = new DataTable("UIData");
            tableUI.Columns.Add("ORDER", typeof(string));
            tableUI.Columns.Add("CO", typeof(string));
            tableUI.Columns.Add("FACTORY", typeof(string));
            tableUI.Columns.Add("STATUS", typeof(string));
            tableUI.Columns.Add("fromdate", typeof(string));
            tableUI.Columns.Add("enddate", typeof(string));
        }

        private void InitDataBinding()
        {
             
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                dwUI = tableUI.NewRow(); 
                InitDataBinding();
                txtORDER.Focus();
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return;

        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet setResult = new DataSet();            
                setUI.Clear();
                setUI.AcceptChanges();
                string ORDER = m_Data.Right("000000"+txtORDER.Text.Trim(),6);
                string CO = txtCO.Text.Trim();
                string FACTORY = txtFACTORY.Text.Trim();
                string STATUS = CheckBox1.IsChecked.Value.ToString();

                if (STATUS == "True")
                {
                    STATUS = "A";
                }
                else
                {
                    STATUS = "B";
                }

                if (FACTORY == "") 
                {
                    FACTORY = "AA";
                }
                if (fromdate == "")
                {
                    fromdate = "20000000";
                }
                if (enddate == "")
                {
                    enddate = "99999999";
                }
                dwUI["ORDER"] = ORDER;
                dwUI["CO"] = CO;
                dwUI["FACTORY"] = FACTORY;
                dwUI["STATUS"] = STATUS;
                dwUI["fromdate"] = fromdate;
                dwUI["enddate"] = enddate;
                setResult = m_Data.BuildFLTDYEORDERData(dwUI);
                setUI.Merge(setResult);
                MasterDg.DataContext = setUI.Tables["DYEORDER"];
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "OpenExcel";
            GenerateReport(PrintType);
        }
        private void GenerateReport(string PrintType)
        {
            try
            {
                m_DataX = new FLTDYEORDERData(dicParamater);
                m_DataX.exAgent._app.Visible = false;
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                FLTDYEORDERReport m_rpt = new FLTDYEORDERReport(dicParamater);
                m_rpt.dsSource = setUI;
                m_rpt.PrintType = "OpenExcel";
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.WriteExcelReport("EXCEL");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                fromdate = "";
                fromdate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void END_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                enddate = "";
                enddate = m_Data.GetDate(sender);
            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter) return;
            btnQuery_Click(null,null);

        }
        
    }
}
